﻿using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Text;

namespace D3Trader
{
    public static class Trader
    {
        // Constants (default settings)
        public const double DefaultLowBid = 7.5;
        public const double DefaultLowBuy = 15;
        public const double DefaultHighBid = 15;
        public const double DefaultHighBuy = 30;
        public const double DefaultSocket1Modifier = 1.25;
        public const double DefaultSocket2Modifier = 1.50;
        public const double DefaultSocket3Modifier = 1.75;

        // App config settings
        public static double LowBid { get; set; }
        public static double LowBuy { get; set; }
        public static double HighBid { get; set; }
        public static double HighBuy { get; set; }
        public static double Socket1Modifier { get; set; }
        public static double Socket2Modifier { get; set; }
        public static double Socket3Modifier { get; set; }

        public static void LoadDefaultSettings()
        {
            // Overwrite runtime settings
            LowBid          = DefaultLowBid;
            LowBuy          = DefaultLowBuy;
            HighBid         = DefaultHighBid;
            HighBuy         = DefaultHighBuy;
            Socket1Modifier = DefaultSocket1Modifier;
            Socket2Modifier = DefaultSocket2Modifier;
            Socket3Modifier = DefaultSocket3Modifier;

            // Save defaults back to the file
            SaveConfig();
        }

        public static void LoadConfig()
        {
            LowBid = Convert.ToDouble(ConfigurationManager.AppSettings["LowBid"]);
            LowBuy = Convert.ToDouble(ConfigurationManager.AppSettings["LowBuy"]);
            HighBid = Convert.ToDouble(ConfigurationManager.AppSettings["HighBid"]);
            HighBuy = Convert.ToDouble(ConfigurationManager.AppSettings["HighBuy"]);
            Socket1Modifier = Convert.ToDouble(ConfigurationManager.AppSettings["Socket1Modifier"]);
            Socket2Modifier = Convert.ToDouble(ConfigurationManager.AppSettings["Socket2Modifier"]);
            Socket3Modifier = Convert.ToDouble(ConfigurationManager.AppSettings["Socket3Modifier"]);
        }

        public static void SaveConfig()
        {
            Configuration config = ConfigurationManager.OpenExeConfiguration(ConfigurationUserLevel.None);
            KeyValueConfigurationCollection settings = config.AppSettings.Settings;

            // Update fields
            settings["LowBid"].Value = LowBid.ToString();
            settings["LowBuy"].Value = LowBuy.ToString();
            settings["HighBid"].Value = HighBid.ToString();
            settings["HighBuy"].Value = HighBuy.ToString();
            settings["Socket1Modifier"].Value = Socket1Modifier.ToString();
            settings["Socket2Modifier"].Value = Socket1Modifier.ToString();
            settings["Socket3Modifier"].Value = Socket1Modifier.ToString();

            // Save the file
            config.Save(ConfigurationSaveMode.Modified);
            // Reload modified section
            ConfigurationManager.RefreshSection(config.AppSettings.SectionInformation.Name);
        }
    }
}
